<template>
  <div class="">
    <div class="container step-block">
      <span>Step1: 选择需要校准的设备</span>
      <el-button @click="next" type="primary">下一步</el-button>
    </div>
    <div class="step-content container">
      <el-table
        :data="tableData"
        border
        v-loading="loading"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column width="100" align="center" label="序号" fixed>
          <template v-slot="scope">
            {{ indexMethod(scope.$index, query.page, query.pageSize) }}
          </template>
        </el-table-column>
        <el-table-column label="全选" type="selection" width="55" />
        <template v-for="item in columns">
          <el-table-column
            :prop="item.prop"
            :label="item.label"
            align="center"
            v-if="item.prop === 'deviceStatus'"
          >
            <template v-slot="scope">
              {{ ["在线", "离线"][scope.row[item.prop]] }}
            </template>
          </el-table-column>
          <el-table-column
            v-else
            :prop="item.prop"
            :label="item.label"
            align="center"
          ></el-table-column>
        </template>
      </el-table>

      <el-pagination
        style="margin-top: 20px"
        background
        layout="prev, pager, next"
        @current-change="query.change"
        :total="query.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script setup lang="ts">
import { indexMethod } from "@/utils/tools"
import { getDeviceCalibrationList } from "@/api"
import { useTool } from "./useTool"
import { Loading } from "@element-plus/icons-vue"
import { ElMessage } from "element-plus"

const { selectIds, selectItem } = useTool()

const query = reactive({
  total: 0,
  pageSize: 10,
  page: 1,
  change: (page) => {
    query.page = page
    getData()
  }
})
useTableHight()
const emits = defineEmits(["change"])

const multipleTable = ref()
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
  selectItem.value = val
  selectIds.value = val.map((i) => i.deviceNo)
  console.log(multipleSelection.value, selectIds.value)
}

const tableData = ref([])
const columns = ref([
  { prop: "deviceBarCode", label: "设备条形码编号" },
  { prop: "deviceNo", label: "设备ID" },
  { prop: "deviceModel", label: "设备型号" },
  { prop: "deviceRange", label: "量程" },
  { prop: "uptime", label: "上线时间" },
  { prop: "produceBatch", label: "生产批次" }
])
const loading = ref(true)
const getData = async () => {
  selectItem.value = []
  loading.value = true
  const res = await getDeviceCalibrationList({
    pageNumber: query.page,
    pageSize: query.pageSize
  })
  loading.value = false
  query.total = res?.total
  tableData.value = res?.rows
  console.log(res)
}

const next = () => {
  if (!selectItem.value.length) {
    ElMessage.warning("请至少选择一个设备")
    return
  }
  emits("change", 1)
}

onMounted(() => {
  getData()
})
</script>
<style lang="less" scoped></style>
